#pragma once
#include <QFile>
#include <QGroupBox>
#include <QLabel>
#include <QMap>
#include <QProgressBar>
#include <QPushButton>
#include <QString>
#include <QVBoxLayout>
#include <QWidget>


class TransferBox : public QGroupBox
{
    Q_OBJECT

public:
    enum Direction { SendDirection, ReceiveDirection, DoneDirection };
    Direction direction;

    TransferBox(int id, QString filename, quint64 filesize, QWidget *parent = 0);
    TransferBox(int id, QString filepath, QWidget *parent = 0);
    ~TransferBox();

    //TransferBox(int id, QWidget *parent = 0);

    void receive(QByteArray bytes);
    QByteArray send();

signals:
    void finishedSending(int id);

private slots:
    void cancelTransfer();
    void pauseTransfer();
    void resumeTransfer();
    void updateProgress();

private:
    void construct(int id, QString filename, TransferBox::Direction dir);
    QString getSavePath(QString filename);

    QPushButton *leftButton, *rightButton;
    QFile *file;

    int id;
    quint64 transferred;
    quint64 fileSize;
    //used for formatting
    quint64 sizeDivisor;
    QString postfix;

    QProgressBar *progressBar;
    QLabel *statusLabel;
};

class FileTransfer : public QWidget
{
    Q_OBJECT

public:
    FileTransfer(QWidget *parent = 0);
    void receiveData(QByteArray data);
private slots:
    void locateFile();
    void removeReceiving(int id);
    void removeSending(int id);
    void loopSend();
signals:
    void sendData(QByteArray data);

private:

    enum TransferFlag { FileDataFlag, FileInitiateFlag, FileFinishFlag };

    QMap<int, TransferBox*> receivingTransfers;
    int sendingIdUpto;
    QMap<int, TransferBox*> sendingTransfers;
    QPushButton *sendFileButton;
    QVBoxLayout *lay;
};
